import cv2 as cv
import sys
import os
from python_ai.common.xcommon import *
import matplotlib.pyplot as plt
import numpy as np
import time
import datetime


def my_show_img(img, title="no title", trans=None, **kwargs):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)
    plt.axis('off')
    plt.title(title)


img_dir = '../../../../../large_data/pic'
img_arr = [
    'opencv_logo.png',
    'IMG_20200819_124310.jpg',
    'HSV.jpg',
    'len_top.jpg',
    'football.jpg'
]
zoom_idx_set = set([1, 3])

spr = 2
spc = 5
spn = 0
plt.figure(figsize=[14, 6])

for i, img_name in enumerate(img_arr):
    sep(i)
    img_path = os.path.join(img_dir, img_name)
    img = cv.imread(img_path, cv.IMREAD_COLOR)
    print('original shape', img.shape)
    my_show_img(img, 'original', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))
    dst = cv.Canny(img, 100, 200)
    print('dst shape', dst.shape)
    my_show_img(dst, 'dst', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))
    if i in zoom_idx_set:
        rate = 700.0 / dst.shape[0]
        zoom = cv.resize(dst, None, fx=rate, fy=rate, interpolation=cv.INTER_LINEAR)
        print('zoom shape', zoom.shape)
        cv.imshow(str(i), zoom)
